package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.DispatchScanMasterTable;
import com.yimi.yinhepda.db.table.DispatchScanTable;
import java.sql.SQLException;
import java.util.List;

/**
 * 集中派送扫描主表
 *
 * @author 000432
 * @date 2017/12/6 0006
 */
public class DispactchScanMasterTableDao extends BaseDao<DispatchScanMasterTable, Long> {

  public DispactchScanMasterTableDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), DispatchScanMasterTable.class);
  }

  /**
   * 判断该任务中的主单号是否存在
   *
   * @param waybillNo 主单号
   * @param mTaskNo 任务号
   * @return 存在返回true
   */
  public boolean exitDataByWaybillNo(Long waybillNo, String mTaskNo) {
    try {
      QueryBuilder<DispatchScanMasterTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("waybillNo", waybillNo).and().eq("taskNo", mTaskNo);
      List<DispatchScanMasterTable> mDataList = queryBuilder.query();
      if (mDataList != null && mDataList.size() > 0) {
        return true;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return false;
  }

  /**
   * 查询任务中的主单号
   */
  public List<DispatchScanMasterTable> getDataByWaybillNo(Long waybillNo, String mTaskNo) {
    try {
      QueryBuilder<DispatchScanMasterTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("waybillNo", waybillNo).and().eq("taskNo", mTaskNo);
      List<DispatchScanMasterTable> mDataList = queryBuilder.query();
      if (mDataList != null && mDataList.size() > 0) {
        return mDataList;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<DispatchScanMasterTable> getWaybillMasterByMaster(String remark, String taskNo) {
    try {
      QueryBuilder<DispatchScanMasterTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("remark", remark);
      where.and().eq("taskNo", taskNo);
      List<DispatchScanMasterTable> mDataList = queryBuilder.query();
      if (mDataList != null && mDataList.size() > 0) {
        return mDataList;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据任务编号删除数据
   */
  public void deleteByTaskNo(DispatchScanTable scanTable) {
    List<DispatchScanMasterTable> scanMasterTableList = getDataByWaybillNo(scanTable.getWaybillNo(), scanTable.getTaskNo());
    if (scanMasterTableList != null) {
      for (DispatchScanMasterTable entity : scanMasterTableList) {
        try {
          delete(entity);
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
